<?php
/**
 * Geneone: Content Management Framework
 * Image File
 *
 * http://geneone.neondragon.net/
 *
 * @package Geneone
 * @author Khlo
 * @version 1.0
 * @copyright 2006 Geneone Development Team
 */
 
class Gene_File_Image extends Gene_File_Common {
	/**
	 * @var resource PHP Image Resource
	 */
	var $imageResource = null;
	
	/**
	 * Returns a PHP image resource of image for manipulation
	 * using the PHP Image functions
	 *
	 * @return resource PHP Image Resource
	 */
	function &getResource() {
		if ($this->imageResource === null) {
			$this->imageResource = imagecreatefromstring($this->fileContent);
		}
		
		return $this->imageResource;
	}
	
	function isSupported() {
		if ($this->getResource()) {
			return true;
		}
		
		return false;
	}
	
	/**
	 * @return integer Height of image in pixels
	 */
	function getHeight() {
		return imagesy($this->getResource());
	}
	
	/**
	 * @return integer Width of image in pixels
	 */
	function getWidth() {
		return imagesx($this->getResource());
	}
	
	/**
	 * Resizes the image.
	 *
	 * @var string $width New width in pixels
	 * @var string $height New height in pixels
	 */
	function resize($width, $height) {
		$oimage = $this->getResource();
		$image = imagecreatetruecolor($width, $height);
		imagecopyresampled($image, $oimage, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
		$this->imageResource = $image;
		imagedestroy($oimage);
	}
	
	/**
	 * Resizes the image to the exact width and height proved whilst maintaining the
	 * aspect ratio by also cropping part of the image.
	 *
	 * @var string $width New width in pixels
	 * @var string $height New height in pixels
	 */
	function resizeCrop($width, $height) {
		$ow = $this->getWidth();
		$oh = $this->getHeight();
		
		if ($width/$ow >= $height/$oh) {
			$enlarge = $width/$ow;
		} else {
			$enlarge = $height/$oh;
		}
		
		$nwidth = round($ow*$enlarge);
		$nheight = round($oh*$enlarge);
		$this->resize($nwidth, $nheight);
		
		$image = imagecreatetruecolor($width, $height);
		$oimage = $this->getResource();
		imagecopy($image, $oimage, 0, 0, ($nwidth-$width)/2, ($nheight-$height)/2, $width, $height);
		$this->imageResource = $image;
		imagedestroy($oimage);
	}
	
	/**
	 * Resizes the image maintaining the aspect ratio. 
	 *
	 * @var string $width Maximum width in pixels
	 * @var string $height Maximum height in pixels
	 */
	function resizeAspect($width, $height) {
		$ow = $this->getWidth();
		$oh = $this->getHeight();
		
		// This looks at what the enlargement ratio would be if expanded to desired
		// width and what it would be if expanded to desired height. The lowest
		// enlargement ratio is allowed because otherwise one of the dimensions will
		// be too big.
		
		if ($width/$ow >= $height/$oh) {
			$enlarge = $height/$oh;
		} else {
			$enlarge = $width/$ow;
		}
		
		$this->resize(round($ow*$enlarge), round($oh*$enlarge));
	}
	
	/**
	 * Makes the image grayscale
	 */
	function grayscale() {
		$height = $this->getHeight();
		$width = $this->getWidth();
		
		/* Convert to True Color */
		$oimage = $this->getResource();
		$image = imagecreatetruecolor($width, $height);
		imagecopy($image, $oimage, 0, 0, 0, 0, $width, $height);
		
		/* Make Grey */
		for ($y = 0; $y < $height; $y++) {
			for ($x = 0; $x < $width; $x++) {
				$grey = (imagecolorat($image, $x, $y) >> 8) & 0xFF;
				imagesetpixel ($image, $x, $y, imagecolorallocate($image, $grey, $grey, $grey));
			}
		}
		
		$this->imageResource = $image;
		imagedestroy($oimage);
	}
	
	/**
	 * This will save the content from the image resource into
	 * $this->fileContent. If you use image manip functions make
	 * sure you call this at the end before saving.
	 */
	function updateContentFromResource() {
		ob_start(); // Eww!!!
		if ($this->fileInfo['mime_type'] == "image/gif") {
			if (function_exists("imagegif")) {
				imagegif($this->getResource());
			} else {
				imagepng($this->getResource());
				$this->fileInfo['mime_type'] = "image/png";
			}
		} else if ($this->fileInfo['mime_type'] == "image/png") {
			imagepng($this->getResource());
		} else {
			$this->fileInfo['mime_type'] = "image/jpeg";
			imagejpeg($this->getResource());
		}
		
		$this->fileContent = ob_get_contents();
		ob_end_clean();
		
		$this->fileInfo['size'] = strlen($this->fileContent);
	}
	
	/**
	 * Sets the content of the file.
	 *
	 * @var string $content File Content
	 */
	function setContent($content) {
		parent::setContent($content);
		$this->imageResource = imagecreatefromstring($content);
	}
}
 
?>